package com.foreveross.dao.imp;

import java.util.List;

import org.springframework.stereotype.Component;

import com.foreveross.common.SQLUtil;
import com.foreveross.dao.HrHouseInfoDAO;
import com.foreveross.dao.HrNewsInfoDAO;
import com.foreveross.vo.HouseUser;
import com.foreveross.vo.HrHouses;
import com.foreveross.vo.HrNewsInfo;
import com.foreveross.vo.Tenement;

@Component("hrHouseInfoDAO")
public class HrHouseInfoDAOImp extends SQLUtil implements HrHouseInfoDAO {

	@Override
	public long findHouseListCount(String id, String proType, String province,String userId) throws Exception {
		StringBuffer queryString = new StringBuffer(
				"select count(*) from HrHouses model where 1=1 ");
		if (!"".equals(id) && null != id) {
			queryString.append(" and model.id = '" + id + "'");
		}
		if (!"".equals(proType) && null != proType) {
			queryString.append(" and model.proType = '" + proType + "'");
		}
		if (!"".equals(province) && null != province) {
			queryString.append(" and model.province = '" + province + "'");
		}
		this.addAuthorityStr(queryString, userId, "id");
		return (Long) findObjectAll(queryString.toString()).get(0);
	}

	@Override
	public void deleteHouse(String id) throws Exception {
		final String sql = "delete from HrHouses t where t.id='" + id + "'";
		updateObejctByHql(sql);
	}

	@Override
	public List<HrHouses> getCityHouseList(String province) throws Exception {
		StringBuffer queryString = new StringBuffer(
				"select model from HrHouses model where 1=1 and model.province='"
						+ province + "'");
		queryString.append(" order by model.id desc");
		return (List<HrHouses>) findObjectAll(queryString.toString());
	}
	

	@Override
	public void editIsDomicile(String weixinId, String userId) throws Exception {
		String sql = "update house_user set isDomicile=0 where wechat_id='"
				+ weixinId + "'";
		updateObejctBySql(sql);
		String sqlv = "update house_user set isDomicile=1 where wechat_id='"
				+ weixinId + "' and id='" + userId + "'";
		updateObejctBySql(sqlv);
	}

	@Override
	public List<HrHouses> findHouseList(String id, String proType,
			String province, int pageIndex, int pageSize,String userId) throws Exception {
		StringBuffer queryString = new StringBuffer(
				"select model from HrHouses model where 1=1 ");
		if (!"".equals(id) && null != id) {
			queryString.append(" and model.id = '" + id + "'");
		}
		if (!"".equals(proType) && null != proType) {
			queryString.append(" and model.proType = '" + proType + "'");
		}
		if (!"".equals(province) && null != province) {
			queryString.append(" and model.province = '" + province + "'");
		}
		this.addAuthorityStr(queryString, userId, "id");
		queryString.append(" order by model.id desc");
		return (List<HrHouses>) findObjectAll(queryString.toString(),
				pageIndex, pageSize);
	}

	@Override
	public Object findObject(Class clazz, String id) throws Exception {
		return this.findById(clazz, id);
	}

	@Override
	public List<HrHouses> findHouseList(String cityId,String userId) throws Exception {
		StringBuffer str = new StringBuffer(
				"select model from HrHouses model where 1=1");
		if (cityId != null && !cityId.isEmpty())
			str.append(" and model.province.codeId='").append(cityId)
					.append("'");
	    this.addAuthorityStr(str, userId, "id");
		return findObejctByHQL(str.toString());
	}
	
	@Override
	public List<HrHouses> findHousesListWithOutTenement(String province,String userId) throws Exception {
		StringBuffer str = new StringBuffer(
				"select model from HrHouses model where 1=1 ");
		str.append("and model.id not in(select houseId from Tenement) ");
		if (province != null && !province.isEmpty())
			str.append(" and model.province.codeId='").append(province)
					.append("'");
	    this.addAuthorityStr(str, userId, "id");
		return findObejctByHQL(str.toString());
	}

	@Override
	public void setTenementHouse(Tenement t, String id) throws Exception {
		String sql = "select model from HouseUser where id in (select th.hrHouses.id from TenementHouse th where th.tenement.id='"
				+ id + "')";
		List<HrHouses> list = findObejctByHQL("select model from HrHouses model where 1=1");
		StringBuffer str = new StringBuffer();
		for (int i = 0; i < list.size(); i++) {
			HrHouses h = list.get(i);
			str.append(h.getId()).append(",");
		}

	}

	@Override
	public List<HouseUser> findHouseUser(String id) throws Exception {
		String sql = "select model from HouseUser model where  model.hrHouses.id='"
				+ id + "'";
		List<HouseUser> list = findObejctByHQL(sql);
		return list;
	}

	@Override
	public void updateHrHousesProjectId(String hrHousesId, String projectId)
			throws Exception {
		String sql = "update hr_houses set project_id='" + projectId
				+ "' where id='" + hrHousesId + "'";
		updateObejctBySql(sql);
	}
}
